package com.haitao.jt808.controller;

import com.haitao.jt808.entity.gpsdata.Data0x8300;
import com.haitao.jt808.config.ChannelManager;
import com.haitao.jt808.entity.gpsdata.DataPackage;
import com.haitao.jt808.handler.BaseHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: sanhoitou[sanhoitou@hotmail.com]
 * @Date: 2023/6/28
 * 广播tts
 */
@RestController
@Api(tags = "下发指令")
public class BroadcastController extends BaseHandler{
    private static final Logger log = LoggerFactory.getLogger(BroadcastController.class);

    @Autowired
    private ChannelManager channelManager;

    @PostMapping("/issueTts")
    @ApiOperation(value = "下发TTS广播")
    public String issueTts(String terminalPhone, String text){
        Channel channel = channelManager.get(terminalPhone);
        Integer flowId = getSerialNumber(channel);
        DataPackage msg = Data0x8300.response(terminalPhone, flowId, text);
        ChannelFuture channelFuture = channel.writeAndFlush(msg);
        boolean done = channelFuture.isDone();
        return !done?"发送成功":"发送失败";
    }
}
